package br.com.winesys.geta.databinding.component.table;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;
import br.com.winesys.geta.editor.Editor;

public class ListSelector<Model> extends JPanel {

	private TableViewer<Wrap<Model>> table;
	private TableModel<Wrap<Model>> tableModel;
	private JPanel botoesPanel;
	private JButton todosButton;
	private JButton nenhumButton;
	private JButton inverterButton;
	private List<Wrap<Model>> values = new ArrayList<Wrap<Model>>();

	public ListSelector() {
		this.setLayout(new BorderLayout());
		this.table = new TableViewer<Wrap<Model>>() {

			@Override
			protected Editor<?> createFilter() {
				return null;
			}

			@Override
			protected TableModel<Wrap<Model>> createTableModel() {
				TableModel<Wrap<Model>> result = new TableModel<Wrap<Model>>() {

					@Override
					protected void createColumns() {
						this.addColumn(ColumnFactory.createBooleanColumn("selecionado", "", 5));
						this.addColumn(ColumnFactory.createReadOnlyStringColumn("valor", "", 90));
					}
				};
				result.setEditing(true);
				return result;
			}

			@Override
			protected JPanel createFindPanel() {
				return null;
			}
		};
		this.table.setTableHeader(null);
		this.add(this.table, BorderLayout.CENTER);

		this.botoesPanel = new JPanel();
		this.botoesPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		this.todosButton = new JButton("Todos");
		this.todosButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				for (Wrap<Model> wrap : ListSelector.this.values) {
					wrap.setSelecionado(true);
				}
				ListSelector.this.table.repaint();
			}
		});
		this.nenhumButton = new JButton("Nenhum");
		this.nenhumButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				for (Wrap<Model> wrap : ListSelector.this.values) {
					wrap.setSelecionado(false);
				}
				ListSelector.this.table.repaint();
			}
		});
		this.inverterButton = new JButton("Inverter");
		this.inverterButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				for (Wrap<Model> wrap : ListSelector.this.values) {
					wrap.setSelecionado(!wrap.getSelecionado());
				}
				ListSelector.this.table.repaint();
			}
		});
		this.botoesPanel.add(this.todosButton);
		this.botoesPanel.add(this.nenhumButton);
		this.botoesPanel.add(this.inverterButton);
		this.add(this.botoesPanel, BorderLayout.SOUTH);
	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		this.tableModel.setEditing(enabled);
	}

	public void setValues(Model[] values) {
		this.values.clear();
		for (Model obj : values) {
			this.values.add(new Wrap<Model>(obj));
		}
		this.table.setValues(this.values);
	}

	public void setSelected(List<Model> selected) {
		for (Wrap<Model> wrap : this.values) {
			boolean achou = false;
			for (Model o : selected) {
				if (o.equals(wrap.getObject())) {
					achou = true;
					break;
				}
			}
			wrap.setSelecionado(achou);
			this.table.invalidate();
		}
	}

	public List<Model> getSelected() {
		List<Model> resultado = new ArrayList<Model>();
		for (Wrap<Model> wrap : this.values) {
			if (wrap.getSelecionado()) {
				resultado.add(wrap.getObject());
			}
		}
		return resultado;
	}
}
